package com.qf.leadnewsarticle.controller.v1;

import com.alibaba.fastjson.JSON;
import com.qf.leadnewsarticle.model.dtos.HomePageDto;
import com.qf.leadnewsarticle.service.ApArticleService;
import com.qf.leadnewsarticle.service.RedisService;
import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.pojos.article.ApArticle;
import com.qf.leadnewsutils.commons.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private RedisService redisService;

    @PostMapping("/load")
    public ResponseResult load(@RequestBody HomePageDto homePageDto){
        /*//测试uid的获取
        Long uid = ThreadLocalUtil.getUid();
        //输出debug级别的日志信息，应用上线是，屏蔽该类信息。流程的关键信息可以设置为info或者更高级别，效率更高
        log.debug("=========登录用户的uid：{}=========",uid);
        return apArticleService.load(homePageDto);*/

        String tag = homePageDto.getTag();
        if ("__all__".equals(tag)){
            tag = "all";
        }

        //从redis中获取指定频道的缓存数据
        String jsonStr = redisService.getCache(tag);
        List<ApArticle> apArticles = JSON.parseArray(jsonStr, ApArticle.class);

        return ResponseResult.okResult(apArticles);
    }

    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody HomePageDto homePageDto){
        return apArticleService.loadnew(homePageDto);
    }

    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody HomePageDto homePageDto){
        return apArticleService.loadmore(homePageDto);
    }
}
